<?php

namespace app\admin\model;

use think\Db;
use think\Model;


class VLesson extends Model
{


    // 表名
    protected $name = 'v_lesson';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_free_text'
    ];


    public function getIsFreeList()
    {
        return ['0' => __('Is_free 0'), '1' => __('Is_free 1')];
    }


    public function getIsFreeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_free']) ? $data['is_free'] : '');
        $list = $this->getIsFreeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    /**
     * 读取分类类型
     * @return array
     */
    public static function getTypeList()
    {
        $v_course_db = Db::name("v_course");
        $list = $v_course_db->field("id,title")->select();

        $typeList = [];
        foreach ($list as $v) {
            $typeList[$v['id']] = $v['title'];
        }
        return $typeList;
    }

    static function getLessonCount($course_ids)
    {
        $list = self::where('v_course_id', 'in', $course_ids)
            ->group('v_course_id')
            ->column('count(id) count', 'v_course_id');
        log_print(self::getLastSql());
        return $list;
    }

}
